//
//  RZQScanViewController.m
//  LastOne
//
//  Created by dllo on 15/11/11.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "RZQScanViewController.h"
#import "FitHeader.h"
#import "RZQDataCollectionViewCell.h"
#import "CoreDataManager.h"
#import "RZQLikeSave.h"
#import "CirleModel.h"
#import "RZQFindWebViewController.h"
#import "NightHeader.h"
@interface RZQScanViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSArray *dataSource;
@property(nonatomic, strong)CoreDataManager *mannager;

@end

@implementation RZQScanViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout *dataFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    dataFlowLayout.minimumInteritemSpacing = 5 ;
    dataFlowLayout.minimumLineSpacing = 5;
    dataFlowLayout.sectionInset = UIEdgeInsetsMake(5 , 5 , 5, 5 );
    _dataSource = [NSMutableArray array ];
    _mannager = [[CoreDataManager alloc]init];
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 113 * FitHeight) collectionViewLayout:dataFlowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    [_collectionView NightWithType:UIViewColorTypeNormal];
    [_collectionView registerClass:[RZQDataCollectionViewCell class] forCellWithReuseIdentifier:@"rzqIdentifier"];
    [self selectAction];

    // Do any additional setup after loading the view.
}
-(void)selectAction
{
   NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"RZQLikeSave"];
    _dataSource = [_mannager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
 
  
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return  CGSizeMake(ScreenWidth / 2 - 10 * FitWidth  , 200 * FitHeight);
    
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RZQDataCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"rzqIdentifier" forIndexPath:indexPath];
    CirleModel *model = [[CirleModel alloc]init];
    
    RZQLikeSave *likeSave = _dataSource[indexPath.row];
    
    NSDictionary *dic = (NSDictionary *)[NSKeyedUnarchiver unarchiveObjectWithData:likeSave.category];
    
    model.category = dic;
    model.title = likeSave.title;
    model.publish_time = [likeSave.publish_time integerValue];
    model.comment_count = [likeSave.comment_count integerValue];
    model.praise_count = [likeSave.praise_count integerValue];

    cell.cirData = model;
    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSource.count;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.hidesBottomBarWhenPushed = YES;
    self.navigationController.navigationBar.hidden = YES;
    RZQFindWebViewController *web = [[RZQFindWebViewController alloc]init];
    [self.navigationController pushViewController:web animated:YES];
     RZQLikeSave *likeSave = _dataSource[indexPath.row];
    CirleModel *model = [[CirleModel alloc]init];
    model.iD = [likeSave.iD integerValue];
    model.title = likeSave.title;
    web.modelData = model;
    self.hidesBottomBarWhenPushed = NO;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
